/*
 * Copyright 2011 Clean GWT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleanform.gwt.chinesechess.client;

import com.cleanform.gwt.bootstrap.client.ui.GroupFormPanel;
import com.cleanform.gwt.bootstrap.client.ui.HeadingPanel;
import com.cleanform.gwt.bootstrap.client.ui.HeadingPanel.PanelType;
import com.cleanform.gwt.bootstrap.js.client.validation.DefaultValidator;
import com.cleanform.gwt.chinesechess.client.page.GameHall;
import com.cleanform.gwt.chinesechess.shared.action.Actions;
import com.cleanform.gwt.page.client.Application;
import com.cleanform.gwt.user.client.ui.Button;
import com.cleanform.gwt.user.client.ui.PasswordTextBox;
import com.cleanform.gwt.user.client.ui.TextBox;
import com.cleanform.gwt.view.client.DefaultPendingHandler;
import com.cleanform.gwt.web.client.ObjectCallback;
import com.cleanform.gwt.web.client.Requests;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;

import static com.cleanform.gwt.user.client.ui.Layouts.as;
import static com.cleanform.gwt.user.client.ui.Layouts.p;
import static com.cleanform.gwt.user.client.ui.Layouts.center;
/**
 * The core of login view.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class Login extends Composite implements ClickHandler {
	TextBox txtbAccount = new TextBox();
	PasswordTextBox txtbPassword = new PasswordTextBox();
	Button btnQuickLogin = new Button("Quick Login");
	Button btnLogin = new Button("Login");
	Button btnBack = new Button(">>");
	DefaultValidator validator = new DefaultValidator();

	public void onLoad() {
		btnBack.setVisible(Chinesechess.getUser() != null);
		if (Chinesechess.getUser() != null) {
			btnBack.setText("Hello, " + Chinesechess.getUser());
		} else {
			btnBack.setText("");
		}
	}

	public Login() {
		GroupFormPanel form = new GroupFormPanel();
		HeadingPanel shell = new HeadingPanel(PanelType.DEFAULT);

		shell.setCaption("<center>GWT Chinese Chess</center>");
		shell.setContentWidget(form);
		initWidget(center(shell));
		form.add(p(btnBack));
		form.build().add("Account", txtbAccount).add("Password", txtbPassword);
		form.add(as(p(btnLogin, btnQuickLogin)).right());
		shell.setSize("320px", "");
		setHeight("520px");
		setWidth("100%");

		btnLogin.setEnabled(false);
		txtbAccount.setEnabled(false);
		txtbPassword.setEnabled(false);

		validator.constraint(txtbAccount).notNull();
		// //validator.bind(txtbPassword).required();
		this.btnQuickLogin.addClickHandler(this);
		this.btnLogin.addClickHandler(this);
		btnBack.addClickHandler(this);
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == this.btnQuickLogin) {
			Requests.postJson("gameController/quickLogin.do", null, new ObjectCallback<String>(String.class,
					DefaultPendingHandler.BLOCKED) {

				@Override
				protected void onCallback(String response) {
					Chinesechess.setUser(response);
					Application.redirect(GameHall.class.getSimpleName());
				}
			});
		} else if (event.getSource() == this.btnLogin) {
			if (validator.validate()) {
				// SimpleObject payload = new SimpleObject();
				// payload.put("account", this.txtbAccount.getValue());
				// payload.put("password", this.txtbPassword.getValue());
				final String clientid = txtbAccount.getValue();
				String url = Requests.buildUrl(Actions.GAME_CONTROLLER, Actions.LOGIN);
				Requests.postObject(url, clientid, new ObjectCallback<String>(String.class,DefaultPendingHandler.BLOCKED) {

					@Override
					protected void onCallback(String response) {
						Chinesechess.setUser(clientid);
						Application.redirect("GameHall");
					}
				});
			}
		} else if (event.getSource() == btnBack) {
			Application.redirect(GameHall.class.getSimpleName());
		}
	}

}
